﻿using OSSUtility.Device;
using SharpDX.Direct2D1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ozone.Device
{

    /// <summary>
    /// IKA艾卡磁力加热搅拌器
    /// </summary>
    public class DevIKAMagneticStirrer : DevBase
    {

        //com4
        //波特率9600
        //校验位  奇校验 ODD
        //数据位  7bit 
        //停止位  1bit


        //COM
        public DevIKAMagneticStirrer(int CommType, string FSrvIP, int FPort) : base(CommType, FSrvIP, FPort)
        {

        }

        /// <summary>
        /// 获取IKA艾卡磁力加热搅拌器产品型号
        /// </summary>
        /// <returns></returns>
        public string getDevInfo()
        {
            try
            {
                //请求IN_NAME\r\n
                //返回C-MAG HS7
                string command = "IN_NAME\r\n";
                string recvStr = session_.sendAndReadStr(command);
                return recvStr;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取加热盘当前温度
        /// </summary>
        /// <returns></returns>
        public string GetCurTemperature()
        {
            string Nowtemperature = string.Empty;
            //请求 IN_PV_2\r\n
            //返回值26.0 2
            try
            {
                string command = "IN_PV_2\r\n";
                string recvStr = session_.sendAndReadStr(command);
                if (recvStr != "" && recvStr.Contains("2"))
                {
                    string[] ArryrecvStr = recvStr.Split(' ');
                    if (ArryrecvStr.Length == 2 && ArryrecvStr[1].ToString().Contains("2"))
                    {
                        Nowtemperature = ArryrecvStr[0].ToString();
                    }
                }
            }
            catch (Exception)
            {
                Nowtemperature = string.Empty;
                throw;
            }
            return Nowtemperature;
        }

        /// <summary>
        /// 获取额定温度值 (既设定的温度值)
        /// </summary>
        /// <returns></returns>
        public string GetRatedTemperature()
        {
            string RatedTemperature = string.Empty;
            //请求 IN_SP_1\r\n
            //返回值285.0 1
            try
            {
                string command = "IN_SP_1\r\n";
                string recvStr = session_.sendAndReadStr(command);
                if (recvStr != "" && recvStr.Contains("1"))
                {
                    string[] ArryrecvStr = recvStr.Split(' ');
                    if (recvStr.Length == 2 && recvStr[1].ToString() == "1")
                    {
                        RatedTemperature = recvStr[0].ToString();
                    }
                }
            }
            catch (Exception)
            {
                RatedTemperature = string.Empty;
                throw;
            }
            return RatedTemperature;
        }



        /// <summary>
        /// 设置加热盘温度
        /// 
        /// </summary>
        public bool SetTemperature(int Temperature)
        {
            //请求
            //OUT_SP_1 x
            //(x= 0...500)
            //返回  无返回值
            bool SetState = false;
            try
            {
                if (Temperature > 0 && Temperature < 500)
                {
                    string command = "OUT_SP_1 " + Temperature + "\r\n";
                    string recvStr = session_.sendAndReadStr(command, 1000);
                    SetState = true;
                }
                else
                {
                    SetState = false;
                }
            }
            catch (Exception)
            {
                SetState = false;
                throw;
            }

            return SetState;
        }

        /// <summary>
        /// 加热盘开关
        /// </summary>
        /// <param name="SwitchState"></param>
        public void HeatingPlateSwitch(bool SwitchState)
        {

            try
            {
                string command = string.Empty;
                if (SwitchState)
                {
                    //开
                    command = "START_1\r\n";
                }
                else
                {
                    //关
                    command = "STOP_1\r\n";
                }
                string recvStr = session_.sendAndReadStr(command, 1000);
            }
            catch (Exception)
            {

                throw;
            }

        }




        /// <summary>
        /// 获取当前旋转速度
        /// </summary>
        /// <returns></returns>
        public string GetNowSpinSpeed()
        {
            string Speed = string.Empty;
            //请求 IN_PV_4\r\n
            //返回值580.0 4
            try
            {
                string command = "IN_PV_4\r\n";
                string recvStr = session_.sendAndReadStr(command);
                if (recvStr != "" && recvStr.Contains("4"))
                {
                    string[] ArryrecvStr = recvStr.Split(' ');
                    if (ArryrecvStr.Length == 2 && ArryrecvStr[1].ToString().Contains("4"))
                    {
                        Speed = ArryrecvStr[0].ToString();
                    }
                }
            }
            catch (Exception)
            {
                Speed = string.Empty;
                throw;
            }
            return Speed;
        }



        /// <summary>
        /// 获取额定转速 (既设定的转速)
        /// </summary>
        /// <returns></returns>
        public string GetRatedSpinSpeed()
        {
            string RatedSpeed = string.Empty;
            //请求 IN_SP_1\r\n
            //返回值285.0 1
            try
            {
                string command = "IN_SP_4\r\n";
                string recvStr = session_.sendAndReadStr(command);
                if (recvStr != "" && recvStr.Contains("4"))
                {
                    string[] ArryrecvStr = recvStr.Split(' ');
                    if (recvStr.Length == 2 && recvStr[1].ToString() == "4")
                    {
                        RatedSpeed = recvStr[0].ToString();
                    }
                }
            }
            catch (Exception)
            {
                RatedSpeed = string.Empty;
                throw;
            }
            return RatedSpeed;
        }
        /// <summary>
        /// 设置旋转速度
        /// 
        /// </summary>
        public bool SetSpinSpeed(int Speed)
        {
            //请求
            //OUT_SP_4 x
            //(x= 0...1500)
            //返回  无返回值
            bool SetState = false;
            try
            {
                if (Speed > 0 && Speed < 1500)
                {
                    string command = "OUT_SP_4 " + Speed + "\r\n";
                    string recvStr = session_.sendAndReadStr(command, 1000);
                    SetState = true;
                }
                else
                {
                    SetState = false;
                }
            }
            catch (Exception)
            {
                SetState = false;
                throw;
            }

            return SetState;
        }

        /// <summary>
        /// 旋转开关
        /// </summary>
        /// <param name="SwitchState"></param>
        public void SpinSwitch(bool SwitchState)
        {

            try
            {
                string command = string.Empty;
                if (SwitchState)
                {
                    //开
                    command = "START_4\r\n";
                }
                else
                {
                    //关
                    command = "STOP_4\r\n";
                }
                string recvStr = session_.sendAndReadStr(command, 1000);
            }
            catch (Exception)
            {

                throw;
            }

        }

    }
}
